import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex);//vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
    state:{//存放状态
        cartList:uni.getStorageSync('cartList')||[]
    },
    mutations:{
      add(state,goods){
        // console.log(13,goods);
        // console.log(15,state.cartList.findIndex(item=>item===goods)===-1);
        if(state.cartList.findIndex(item=>item.goods_id===goods.goods_id)===-1){
          state.cartList.push({...goods,goods_num:1,checked:true})
        }else{
          state.cartList[state.cartList.findIndex(item=>item.goods_id===goods.goods_id)].goods_num++
        }
      },
      setCart(state,list){
        state.cartList=list
        uni.setStorageSync('cartList',list)
      }
    }
})
export default store